package com.cgtong.android.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cgtong.android.R;
import com.cgtong.android.adapter.RecordAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.RecordBean;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.GetScorePageContract;
import com.cgtong.android.mvp.presenter.GetScorePagePresenter;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.RecycleViewDivider;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhouyou.http.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 记录查询
 */
public class MyRecordsActivity extends BaseTitleActivity implements GetScorePageContract.View {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;

    private List<RecordBean> mList = new ArrayList<>();
    private BaseQuickAdapter<RecordBean, BaseViewHolder> mAdapter;
    private GetScorePagePresenter getScorePagePresenter;
    private int maxId;

    @Override
    protected String getContentTitle() {
        return getString(R.string.txt_records_title);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_advise;
    }

    @Override
    protected void loadDataByNet() {
        initView();
        initData();
    }

    @Override
    protected void onDataReloadByNet(View v) {
    }

    private void initView() {
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                maxId = 0;
                mList.clear();
                getScorePagePresenter.getScoreDatas(maxId, ConstantManager.MAX_WONDERFUL_NEWS_COUNT, true);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new RecordAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, R.drawable.shape_line));
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                int lastId = mAdapter.getData().get(mAdapter.getData().size() - 1).getId();
                getScorePagePresenter.getScoreDatas(lastId, ConstantManager.MAX_WONDERFUL_NEWS_COUNT, false);
            }
        }, mRecyclerView);
    }

    private void initData() {
        getScorePagePresenter = new GetScorePagePresenter(this);
        getScorePagePresenter.getScoreDatas(maxId, ConstantManager.MAX_WONDERFUL_NEWS_COUNT, true);
    }

    @Override
    public void setScoreDatas(final boolean isRefresh, final List<RecordBean> list) {
        PostUtil.postSuccessDelayed(loadService);
        if (isRefresh) {
            mAdapter.setNewData(list);
            mAdapter.disableLoadMoreIfNotFullPage(mRecyclerView);
        } else {
            mAdapter.addData(list);
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        mSwipeLayout.setRefreshing(isRefresh);
    }

    @Override
    public void setEnableLoadMore(boolean enableLoadMore) {
        mAdapter.setEnableLoadMore(enableLoadMore);
    }

    @Override
    public void onLoadMoreEnd(boolean isLoadMoreEnd) {
        mAdapter.loadMoreEnd(isLoadMoreEnd);
    }

    @Override
    public void onError(ApiException e) {
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }


    @Override
    public void onNullData() {
//        mAdapter.setEmptyView(R.layout.layout_empty);
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }

}
